<template>
    <van-cell
            class="cart-item"
    >
        <van-checkbox v-model="itemChecked" checked-color="#ee0a24" icon-size="0.5rem" class="select"></van-checkbox>
        <div class="detail" @click="handleRouter">
            <img :src="item.productInfo.image">
            <div class="info">
                <p class="title show-in-one-lines">{{item.productInfo.store_name}}</p>
                <p class="price-and-number">
                    <span class="price">￥{{item.truePrice}}</span>
                    <van-stepper v-model="count" :max="item.trueStock" @click.stop></van-stepper>
                </p>
                <span class="delete">删除</span>
            </div>
        </div>
    </van-cell>
</template>

<script setup>
  import { defineProps, computed, ref } from 'vue'
  import { useStore } from 'vuex'
  import { useRouter } from 'vue-router'

  const store = useStore()
  const router = useRouter()

  const { item } = defineProps({
    item: {
      type: Object,
      required: true,
    }
  })
  const _itemChecked = ref(item.checked)
  const itemChecked = computed({
    get() {
      return _itemChecked.value
    },
    set(checked) {
      console.log(item.id, checked)
      _itemChecked.value = checked
      store.commit('cartCheckedChange', { id: item.id, checked })
    }
  })

  const count = computed({
    get() {
      return item.cart_num
    },
    set(count) {
      // store.commit('cartCountChange', { id: item.id, count })
      store.dispatch('cartCountChange', { id: item.id, count })
    }
  })

  const handleRouter = () => {
    router.push({
      name: 'product',
      params:
        {
          productId: item.product_id,
        }
    })
  }
</script>

<style lang="scss" scoped>
    .cart-item :deep(.van-cell__value) {
        display: flex;

        .select {
            width: 20px;
            position: relative;
            top: -10px;
        }

        .detail {
            flex: 1;
        }
    }

    .detail {
        display: flex;

        img {
            width: 88px;
            height: 88px;
            padding-left: 5px;
        }

        .info {
            margin-left: 5px;
        }

        .title {
            margin: 5px 0 15px;
        }

        .price-and-number {
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;

            .price {
                font-size: 16px;
                color: #F2270C;
            }
        }

        .delete {
            float: right;
            display: block;
            padding: 0px 10px 3px;
        }
    }
</style>
